package com.project.controller;

import com.project.bean.InfoBean;


import com.project.bean.MQBean;
import com.project.bean.TianMaoBean;
import com.project.remoteService.IInfoService;
import com.project.service.ITianMaoService;
import com.project.service.impl.RabbitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("tianmao")
@Api(tags = "远程调用接口")
public class TianMaoController {
     @Autowired
    ITianMaoService tianMaoService;
     @Autowired
     IInfoService infoService;
     @Autowired
     RabbitService rabbitService;


//    @ApiOperation(value = "查")
//    @GetMapping("findByName/{name}")
//    public String findByName(@PathVariable("name") String name){
//
//        List<MovieBean> list = service.findByName(name);
//        TianMaoBean tianMao = new TianMaoBean();
//        for (MovieBean movie:list){
//            tianMao.setDate(movie.getDate());
//            tianMao.setEndDate(movie.getEndDate());
//            tianMao.setImg(movie.getImg());
//            tianMao.setName(movie.getName());
//            tianMao.setMovieName(movie.getMovieName());
//            tianMao.setNum(movie.getNum());
//            tianMao.setPrice(movie.getPrice());
//            tianMaoService.add(tianMao);
//        }
//        return "ok";
//    }
    @ApiOperation("购买")
    @GetMapping("byMovie/{id}/{num}")
    public String byMovie(@PathVariable("id") Integer id,@PathVariable("num") Integer num){
        TianMaoBean tianMao= tianMaoService.findById(id);
        InfoBean info =  new InfoBean();
        info.setName(tianMao.getName());
        info.setPrice(tianMao.getPrice());
        info.setNum(num);
        info.setMovieName(tianMao.getMovieName());
        info.setOrderNum(String.valueOf(System.currentTimeMillis()));
        tianMaoService.update(id,tianMao.getNum()-num);
        infoService.add(info);
        return "ok";
    }
    @ApiOperation("查询订单")
    @GetMapping("findAllInfo")
    public List<InfoBean> findAllInfo(){
        return infoService.findAll();
    }
    @ApiOperation("查询全部")
    @GetMapping("findAll")
    public List<TianMaoBean> findAll(){
        return tianMaoService.findAll();
    }

//    @ApiOperation("修改")
//    @PostMapping("update")
//    public String update(@RequestBody InfoBean info){
//        rabbitService.sendUser(info);
//       return "ok";
//
//    }

    @ApiOperation("name查询")
    @GetMapping("findByName/{name}")
    public List<TianMaoBean> findById(@PathVariable("name") String name){
        return tianMaoService.findByName(name);
    }
    @ApiOperation("name修改")
    @PostMapping ("add")
    public String addMQ(@RequestBody MQBean mqBean){
        rabbitService.sendUser(mqBean);
        return "ok";
    }
}
